package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.dao.health.SysMenuMapper;
import com.angel.health.SysMenu;
import com.angel.health.SysMenuExt;
import com.angel.health.SysRoleExt;
import com.angel.health.SysUser;
import com.angel.provider.sys.SysManagerMenuProvider;

/**
 * 高亚林
 * 2016年9月21日下午3:00:05
 */

@Service
public class SysManagerMenuProviderImpl extends BaseProviderImpl<SysMenu> implements SysManagerMenuProvider{

	@Autowired
	private SysMenuMapper mapper;
	
	//根据用户查询菜单
	public List<SysMenu> findAllMenu(SysUser user) {
		return mapper.findAllMenu(user);
	}

	//删除单个菜单
	public int deleteById(SysMenu menu) {
		menu.setDelFlag("0");
		int i = mapper.updateByPrimaryKeySelective(menu);
		return i;
	}

	//保存
	public Map<String, String> save(SysMenu menu) {
		Map<String, String> map = new HashMap<String, String>();
		if(menu == null || menu.getName() == null || menu.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		SysMenu sysMenu = new SysMenu();
		sysMenu.setName(menu.getName());
		List<Map<String, Object>> list = mapper.findAll(sysMenu);
		if(list != null && list.size() > 0){
			map.put("msg", "菜单已存在");
			map.put("code", "0");
			return map;
		}
		menu.setCreateTime(new Date());
		menu.setDelFlag("1");
		mapper.insert(menu);
		if(menu.getParentId() == null){
			menu.setParentId(-1);
			menu.setParentIds("-1");
			SysMenu menu1 = new SysMenu();
			menu1.setParentId(-1);
			List<Map<String, Object>> list2 = mapper.findAll(menu1);
			if(list2 == null || list2.size() < 1){
				menu.setSort(100);
			}else{
				Integer sort = (Integer) list2.get(list2.size() - 1).get("sort");
				menu.setSort(sort + 100);
			}
		}else{
			Integer parentId = menu.getParentId();
			SysMenu menu2 = new SysMenu();
			menu2.setId(parentId);
			List<Map<String, Object>> list3 = mapper.findAll(menu2);
			if(list3 == null || list3.size() < 1){
				map.put("msg", "请先添加上级菜单");
				map.put("code", "0");
				return map;
			}
			String parentIds = (String) list3.get(0).get("parentIds");
			Integer sort = (Integer) list3.get(0).get("sort");
			if("-1".equals(parentIds)){
				menu.setParentIds(menu.getParentId().toString());
			}else{
				menu.setParentIds(parentIds + "-" + menu.getParentId());
			}
			mapper.updateByPrimaryKeySelective(menu);
			String parentIds2 = menu.getParentIds();
			if(parentIds2.indexOf("-") < 1){
				SysMenu menu3 = new SysMenu();
				menu3.setParentId(parentId);
				List<Map<String, Object>> list4 = mapper.findAll(menu3);
				if(list4 == null || list4.size() < 2){
					menu.setSort(sort + 10);
				}else{
					Integer sort2 = (Integer) list4.get(list4.size() - 1).get("sort");
					menu.setSort(sort2 + 10);
				}
			}else{
				SysMenu menu4 = new SysMenu();
				menu4.setParentId(parentId);
				List<Map<String, Object>> list5 = mapper.findAll(menu4);
				if(list5 == null || list5.size() < 2){
					menu.setSort(sort + 1);
				}else{
					Integer sort2 = (Integer) list5.get(list5.size() - 1).get("sort");
					menu.setSort(sort2 + 1);
				}
			}
		}
		mapper.updateByPrimaryKeySelective(menu);
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据id查询
	public SysMenuExt findById(SysMenu menu) {
		List<SysMenuExt> list = mapper.findUserById(menu);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	//修改用户
	public Map<String, String> update(SysMenu menu) {
		Map<String, String> map = new HashMap<String, String>();
		if(menu == null || menu.getName() == null || menu.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		menu.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(menu);
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}

	//根据角色查询菜单
	public List<SysMenu> getMenuByRole(Integer roleId) {
		List<SysMenu> list = mapper.findMenuByRole(roleId);
		return list;
	}

	//查询所有菜单
	public List<SysMenu> findMenus(SysMenu menu) {
		return mapper.findMenus(menu);
	}


}
